package com.catal.framework.servlet;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.Container;
import org.directwebremoting.extend.CreatorManager;
import org.directwebremoting.impl.DwrXmlConfigurator;
import org.directwebremoting.servlet.DwrServlet;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import com.catal.framework.util.WebContextUtil;

public class SpringBeanDWRServlet extends DwrServlet{
	
	private static Log log = LogFactory.getLog(SpringBeanDWRServlet.class
	        .getName());

	public void init(ServletConfig servletConfig) throws ServletException {

		super.init(servletConfig);
		
		ApplicationContext context = WebContextUtil.getContext();
		SpringCreator.setOverrideBeanFactory(context);

		try {
//			String[] configs = WebContextUtil.getDwrFiles();
//			for (int i = 0; i < configs.length; i++) {
//				DwrXmlConfigurator system = new DwrXmlConfigurator();
//				system.setClassResourceName(configs[i]);
//				system.configure(getContainer());
//			}
			
			addSpringBeans(context, getContainer());

		} catch (IllegalArgumentException ex) {
			throw new ServletException(ex);
		} catch (InstantiationException ex) {
			throw new ServletException(ex);
		} catch (IllegalAccessException ex) {
			throw new ServletException(ex);
		}

	}

	private void addSpringBeans(ApplicationContext context, Container container)
			throws IllegalArgumentException, InstantiationException,
			IllegalAccessException {
		ConfigurableApplicationContext c = (ConfigurableApplicationContext) context;
		ConfigurableListableBeanFactory beanFactory = c.getBeanFactory();
		String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
		for (int i = 0; i < beanDefinitionNames.length; i++) {
			String beanName = beanDefinitionNames[i];
			BeanDefinition beanDefinition = beanFactory
					.getBeanDefinition(beanName);

			Object dwrBean = beanDefinition.getAttribute("todwr");

			if ("true".equals(dwrBean)) {

				if (beanName.indexOf(".") != -1) {
					log.warn("dwr bean 不能包含特殊字符,beanName="
							+ beanName);
					continue;
				}
				log.debug("add to dwr config.beanName=" + beanName);
				String type = "spring";
				String javascript = beanName;
				Map params = new HashMap();
				params.put("beanName", beanName);
				params.put("javascript", beanName);
				params.put("creator", type);
				

				
				String beanClassName = beanDefinition.getBeanClassName();
				//当有bean 有 parent时,class可能没有设置，此时beanClassName是空的
				//更好的做法是，查找parent的class
				if(beanClassName!=null){
					params.put("class", beanClassName);
				}

				CreatorManager creatorMgr = (CreatorManager) container
						.getBean(CreatorManager.class.getName());
				creatorMgr.addCreator(type,params);
			}
		}

	}
}
